//
//  GetNearedPresonManager.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/12/1.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "GetNearedPresonManager.h"
#import "PresonInfo.h"

@interface GetNearedPresonManager ()
@property(nonatomic,strong)CLLocation *orig;
@property(nonatomic,strong)CLLocation *dist;
@property(nonatomic,assign)CLLocationDistance kilometers;
@end

@implementation GetNearedPresonManager
static GetNearedPresonManager *getManager = nil;
+(instancetype)manager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        getManager = [[GetNearedPresonManager alloc]init];
        
    });
    return getManager;
}

-(void)getNearedPreson:(void(^)(NSMutableArray *result)) callback{
    [self.sourceArray removeAllObjects];
    AVUser *user = [AVUser currentUser];
    AVGeoPoint *userLocation =  (AVGeoPoint *) [user objectForKey:@"location"];
    AVQuery *query = [AVQuery queryWithClassName:@"_User"];
    [query whereKey:@"location" nearGeoPoint:userLocation];
    query.limit = 100;
    NSArray<AVObject *> *posts = [query findObjects];
    [self getPersonInfoWithArray:posts];
    
    callback(self.sourceArray);
}

-(void)getPersonInfoWithArray:(NSArray *)array{
    AVUser *selfUser = [AVUser currentUser];
    for (AVUser *otherUser in array) {
        PresonInfo *preson = [[PresonInfo alloc]init];
        preson.username = otherUser.username;
        preson.userID = otherUser.objectId;
        preson.sex = [otherUser objectForKey:@"sex"];
        preson.indiciduality = [otherUser objectForKey:@"IndividualitySignature"];
        preson.hometown = [otherUser objectForKey:@"hometown"];
        
        AVGeoPoint *otherpoint = [otherUser objectForKey:@"location"];
        AVGeoPoint *userLocation = [selfUser objectForKey:@"location"];
        
        //preson.distance = [GetNearedPresonManager LantitudeLongitudeDist:otherpoint.longitude other_Lat:otherpoint.latitude self_Lon:userLocation.longitude self_Lat:userLocation.latitude];
        preson.distance = [self LocationDistance:otherpoint self:userLocation];
        
        [self.sourceArray addObject:preson];
    }
}

+(double) LantitudeLongitudeDist:(double)lon1 other_Lat:(double)lat1 self_Lon:(double)lon2 self_Lat:(double)lat2{
    double er = 6378137; // 6378700.0f;
    //ave. radius = 6371.315 (someone said more accurate is 6366.707)
    //equatorial radius = 6378.388
    //nautical mile = 1.15078
    double radlat1 = PI*lat1/180.0f;
    double radlat2 = PI*lat2/180.0f;
    //now long.
    double radlong1 = PI*lon1/180.0f;
    double radlong2 = PI*lon2/180.0f;
    if( radlat1 < 0 ) radlat1 = PI/2 + fabs(radlat1);// south
    if( radlat1 > 0 ) radlat1 = PI/2 - fabs(radlat1);// north
    if( radlong1 < 0 ) radlong1 = PI*2 - fabs(radlong1);//west
    if( radlat2 < 0 ) radlat2 = PI/2 + fabs(radlat2);// south
    if( radlat2 > 0 ) radlat2 = PI/2 - fabs(radlat2);// north
    if( radlong2 < 0 ) radlong2 = PI*2 - fabs(radlong2);// west
    //spherical coordinates x=r*cos(ag)sin(at), y=r*sin(ag)*sin(at), z=r*cos(at)
    //zero ag is up so reverse lat
    double x1 = er * cos(radlong1) * sin(radlat1);
    double y1 = er * sin(radlong1) * sin(radlat1);
    double z1 = er * cos(radlat1);
    double x2 = er * cos(radlong2) * sin(radlat2);
    double y2 = er * sin(radlong2) * sin(radlat2);
    double z2 = er * cos(radlat2);
    double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
    //side, side, side, law of cosines and arccos
    double theta = acos((er*er+er*er-d*d)/(2*er*er));
    double dist  = theta*er;
    return dist;
}

-(double)LocationDistance:(AVGeoPoint *)otherpoint
                   self:(AVGeoPoint *)userLocation{
    self.orig = [[CLLocation alloc] initWithLatitude:otherpoint.latitude longitude:otherpoint.longitude];
    self.dist = [[CLLocation alloc] initWithLatitude:userLocation.latitude longitude:userLocation.longitude];
    self.kilometers = [self.orig distanceFromLocation:self.dist]/1000;
    return self.kilometers;
}



-(NSMutableArray *)sourceArray{
    if (_sourceArray == nil) {
        _sourceArray = [NSMutableArray new];
    }
    return _sourceArray;
}




@end
